package com.UI;

import java.util.Calendar;
import java.util.List;

import com.Model.CurricularDbAdapter;
import com.Model.StudentDBModel;
import com.Model.StudyInfoAdapter;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.ListActivity;
import android.app.TimePickerDialog;
import android.content.ContentValues;
import android.content.Intent;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;

/*
 * Author: Sugat Mahanti
 */
public class StudyInfoList extends ListActivity {
	
	private ListView mylist;
	List<RetrieveStudyInfo> records;
	RetrieveStudyInfo[] studyinfo_array;
	String s;
	String[] menuItems = {"Delete Info","Edit Info"};
	protected static final int CONTEXTMENU_DELETEITEM = 0;
	protected static final int CONTEXTMENU_EDITITEM = 1;
	StudentDBModel studyinfo;
	private StudyInfoAdapter studyinfo_adapter;
	private List<String> StudyInfoNames;
	private String[] studyinfo_name;
	private String courseNo;
	private String cNo;
	 @Override
	    public void onCreate(Bundle savedInstanceState) {
	        super.onCreate(savedInstanceState);
	        requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
	        setContentView(R.layout.study_info_list);
	    	getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.study_info_titlebar);
	    	
	    	Bundle extras = getIntent().getExtras();
			 courseNo = extras.getString("CourseNum");
	    	
	    	
	    	((TextView)findViewById(R.id.StudyInfoTitle)).setText("Study Group Info");
	    	
	    	
	    	
	    	studyinfo = new StudentDBModel(this);
	    	
	    	findViewById(R.id.back).setOnClickListener(new OnClickListener() {
	    		public void onClick(View v) {
	    			StudyInfoList.this.finish();

	    		}
	    	});

	    	
	    	
	    	findViewById(R.id.plus).setOnClickListener(new OnClickListener() {
	    		public void onClick(View v) {
	    			StudyInfoList.this.finish();
	    			Intent addStudyInfo = new Intent();
	    			addStudyInfo.putExtra("CourseNum",courseNo);
	    			addStudyInfo.setClassName("com.UI", "com.UI.AddStudyInfo");
	    			addStudyInfo.putExtra("CourseNum",courseNo);
	    			startActivity(addStudyInfo);
	    			
	    		}
	    	});
	    	
	        
	        
	        mylist= getListView();

	        mylist.setTextFilterEnabled(true);

	        refreshStudyInfo();
	        
	        registerForContextMenu(mylist);

	        mylist.setOnItemClickListener(new OnItemClickListener() {
		    	public void onItemClick(AdapterView<?> a, View v, int position, long id) {
		    		cNo = courseNo;
		        	Intent viewEvent = new Intent(StudyInfoList.this, ViewStudyInfo.class);
		        	viewEvent.putExtra("CourseNum", cNo);
		        	startActivity(viewEvent);
		    		
		        	refreshStudyInfo();
		    	}
		 });
	       
	    }
	 
	 	
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 public void onCreateContextMenu(ContextMenu menu, View v,
	         ContextMenuInfo menuInfo) {
	         AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)menuInfo;
	         
	         //menu.setHeaderTitle(todo_arr[info.position]);

	         for (int i = 0; i<menuItems.length; i++) {
	           menu.add(Menu.NONE, i, i, menuItems[i]);
	         }
	     
	       
	       
	     }
	 public boolean onContextItemSelected(MenuItem item) {
	       AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)item.getMenuInfo();
	       int menuItemIndex = item.getItemId();
	       String menuItemName = menuItems[menuItemIndex];
	       String listItemName = studyinfo_name[info.position];

	       // Switch on the ID of the item, to get what the user selected. 

	       switch (menuItemIndex) {

	            	   
	              case CONTEXTMENU_DELETEITEM:
	            	  
	            	  // Remove it from the list.

	            	  studyinfo.deleteStudyInfo(listItemName);
	            	  
	                  refreshStudyInfo();

	                  return true;
	                 
	              case CONTEXTMENU_EDITITEM:
		  		 		
		  	           	Intent editEvent = new Intent(StudyInfoList.this, EditStudyInfo.class);
		  	           	editEvent.putExtra("GroupName", listItemName);
		  	        	startActivity(editEvent);    
		  	        	refreshStudyInfo();  
		  	        	return true;	   
	                      

	       }

	       return false;
	     }
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 public void refreshStudyInfo(){
    	 
 	    records = studyinfo.selectStudyInfo();
 	   StudyInfoNames = studyinfo.selectStudyInfoName();
 	   
 	  studyinfo_name = (String[])StudyInfoNames.toArray(new String[StudyInfoNames.size()]);
 	 
         this.studyinfo_adapter = new StudyInfoAdapter(this, R.layout.study_info_row, records);
         setListAdapter(this.studyinfo_adapter);
 	 
  }
	
}
